<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

class StoryLogic
{
	function __construct()
	{
		
	}
	
	function get_story(&$request)
	{
		
	}
	
	function add_story(&$request)
	{
		$content = $request['content'];
		
		$tagService = new TagService();
		$feedService = new FeedService();
				
		if (false !== strpos($content, '#'))
		{
			if (preg_match_all('~\#([^\#\\\\]+?)\#~', $content, $match))
            {
            	$cont_sch = $cont_rpl = array();
            	
                $i = 0;
                foreach ($match[1] as $v)
                {
                    $v = trim($v);
                    if (($vl = strlen($v)) < 2 || $vl > 50)
                    {
                        continue;
                    }
                    
                    $cont_sch[] = "#{$v}#";
                    $cont_rpl[] = "<T>#{$v}#</T>";
                    
                    $tag = $tagService->get($v);
                    if (!check_array($tag))
                    {
                    	$tid = $tagService->add(array('name'=>$v, 'uid'=>$request['uid']));
                    }
                    else
                    {
                    	$tagService->update_story_count($tag['id']);
                    }
                }
                
                if (check_array($cont_sch))
                {
		            foreach($cont_sch as $k=>$v)
		        	{
		        		if($v && isset($cont_rpl[$k]))
		        		{
		        			$request['content'] = str_replace($v, $cont_rpl[$k], $content);
		        		}
		        	}
                }
            }
		}
		
		# @user
        if (false !== strpos($content, '@'))
        {
            if (preg_match_all('~\@([\w\d\_\-\x7f-\xff]+)(?:[\r\n\t\s ]+|[\xa1\xa1]+)~', $content, $match))
            {
                if (is_array($match[1]) && count($match[1]))
                {
                	$cont_sch = $cont_rpl = array();
                	
                    foreach ($match[1] as $k => $v)
                    {
                        $v = trim($v);
                        if ('　' == substr($v, -2))
                        {
                            $v = substr($v, 0, -2);
                        }

                        if ($v && strlen($v) < 16)
                        {
                            $match[1][$k] = $v;
                            $user = $userService->get($v);
                            if (check_array($user))
                            {
                            	$feedService->add(array('uid'=>$request['uid'],'fuid'=>$user['id'],'type'=>'a', 'time'=>time()));
	                            $cont_sch[] = "@{$user['name']} ";
	                        	$cont_rpl[] = "<M {$user['id']}>@{$user['name']}</M> ";
                            }
                        }
                    }
                    
	                if (check_array($cont_sch))
	                {
			            foreach($cont_sch as $k=>$v)
			        	{
			        		if($v && isset($cont_rpl[$k]))
			        		{
			        			$request['content'] = str_replace($v, $cont_rpl[$k], $content);
			        		}
			        	}
	                }
                }
            }
        }
	}
	
	function update_story(&$request)
	{
		
	}
	
	function delete_story(&$request)
	{
		
	}
	
	// TODO. update feeds
	function add_comment(&$request)
	{
		$content = $request['comment'];
		
		$userService = new UserService();
		
		$feedService = new FeedService();
		
		# @user
        if (false !== strpos($content, '@'))
        {
            if (preg_match_all('~\@([\w\d\_\-\x7f-\xff]+)(?:[\r\n\t\s ]+|[\xa1\xa1]+)~', $content, $match))
            {
                if (is_array($match[1]) && count($match[1]))
                {
                	$cont_sch = $cont_rpl = array();
                	
                    foreach ($match[1] as $k => $v)
                    {
                        $v = trim($v);
                        if ('　' == substr($v, -2))
                        {
                            $v = substr($v, 0, -2);
                        }

                        if ($v && strlen($v) < 16)
                        {
                            $match[1][$k] = $v;
                            $user = $userService->get($v);
                            if (check_array($user))
                            {
                            	$feedService->add(array('uid'=>$request['uid'],'fuid'=>$user['id'],'type'=>'a', 'time'=>time()));
	                            $cont_sch[] = "@{$user['name']} ";
	                        	$cont_rpl[] = "<M {$user['id']}>@{$user['name']}</M> ";
                            }
                        }
                    }
                    
	                if (check_array($cont_sch))
	                {
			            foreach($cont_sch as $k=>$v)
			        	{
			        		if($v && isset($cont_rpl[$k]))
			        		{
			        			$request['comment'] = str_replace($v, $cont_rpl[$k], $content);
			        		}
			        	}
	                }
                }
            }
        }
	}
	
	function get_comment(&$request)
	{
		
	}
}